package com.liuwei.msmall.order.service.impl;

import com.baomidou.mybatisplus.core.conditions.query.QueryWrapper;
import com.baomidou.mybatisplus.core.metadata.IPage;
import com.baomidou.mybatisplus.extension.service.impl.ServiceImpl;
import com.liuwei.msmall.common.utils.DateUtils;
import com.liuwei.msmall.common.utils.PageUtils;
import com.liuwei.msmall.common.utils.Query;
import com.liuwei.msmall.order.dao.PaymentInfoDao;
import com.liuwei.msmall.order.entity.PaymentInfoEntity;
import com.liuwei.msmall.order.service.PaymentInfoService;
import com.liuwei.msmall.order.vo.PayAsyncVo;
import org.springframework.stereotype.Service;

import java.math.BigDecimal;
import java.util.Date;
import java.util.Map;


@Service("paymentInfoService")
public class PaymentInfoServiceImpl extends ServiceImpl<PaymentInfoDao, PaymentInfoEntity> implements PaymentInfoService {

    @Override
    public PageUtils queryPage(Map<String, Object> params) {
        IPage<PaymentInfoEntity> page = this.page(
                new Query<PaymentInfoEntity>().getPage(params),
                new QueryWrapper<PaymentInfoEntity>()
        );

        return new PageUtils(page);
    }

    @Override
    public void savePayMentInfo(PayAsyncVo vo) {
        PaymentInfoEntity paymentInfoEntity = new PaymentInfoEntity();

        paymentInfoEntity.setAlipayTradeNo(vo.getTrade_no());
        paymentInfoEntity.setCallbackTime(DateUtils.stringToDate(vo.getNotify_time(),"yyyy-MM-dd HH:mm:ss"));
        paymentInfoEntity.setConfirmTime(new Date());
        paymentInfoEntity.setCreateTime(new Date());
        paymentInfoEntity.setOrderSn(vo.getOut_trade_no());
        paymentInfoEntity.setSubject(vo.getSubject());
        paymentInfoEntity.setTotalAmount(new BigDecimal(vo.getTotal_amount()));
        paymentInfoEntity.setPaymentStatus("已支付");
        this.baseMapper.insert(paymentInfoEntity);
    }

}